// pages/order/newOrder/newOrder.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show:false,
    params: {},
    sampleNum: '',
    showUrl: '',
    picList: []
  },
  toggle(type, val) {
    this.setData({
      [type]: val
    });
  },
  chooseTicket() {
    wx.navigateTo({
      url: '/pages/my/myTicket/myTicket',
    })
  },
  addNewOrder() {
    let that = this;
    that.toSubscribeMsg();
    if (0 == that.data.picList.length) {
      wx.showToast({
        title: '请上传图片',
        icon: 'none'
      })
      return
    }
    let busOrderPicsList = [];
    for (let i of that.data.picList) {
      busOrderPicsList.push({
        picUrls: i
      })
    }
    let params = that.data.params;
    params.busOrderPicsList = busOrderPicsList;
    
    app.req({
      url: "/order/busOrder/add",
      method: 'post',
      data: params,
      success(res) {
        if (200 == res.code) {
          app.showToast({
            title: '下单成功',
          })
        } else {
          app.showToast({
            title: '下单失败，请重试',
            icon: 'none'
          })
        }
      }
    });
   
   
  },
  //订阅消息
	toSubscribeMsg(){
		console.log("toSubscribeMag")
		var that = this
		//调起客户端小程序订阅消息界面，返回用户订阅消息的操作结果
		wx.requestSubscribeMessage({
		  tmplIds :['66-tv7X7SsoW5YGknytYbidwoGmhKu1VmdxVEtG_Guo','SluI_7F8P0epVm7HZ0xeocb1nC1u-ykoo6dwj22sH2Q'], //需要订阅的消息模板的id的集合，一次调用最多可订阅3条消息
		  success: function(res){
			console.log(res)
			let result = res['66-tv7X7SsoW5YGknytYbidwoGmhKu1VmdxVEtG_Guo']
			console.log('result',result)
			if(result == 'accept'){//允许订阅
			  wx.showToast({
          title: '订阅成功',

        })
			  //服务端调用 消息发送接口 subscribeMessage.send
			  
	
			}else if(result == 'reject'){//取消
			  console.log('拒绝订阅')
      }
              wx.navigateBack({
                delta: 1,
              })
            
		  }
		})
	},

  baseInfo() {
    let that = this;
    app.req({
      url: "/common/static/base",
      success(res) {
        if (200 == res.code) {
          let fapiao = res.result.fapiao;
          // if (fapiao && 0 < fapiao.length) {
          //   that.toggle("params.cusCompany", fapiao[0].title);
          // } else {
          //   that.toggle("params.cusCompany", "");
          // }
        }
      }
    })
  },
  changeValue(e) {
  this.toggle(e.currentTarget.dataset.param, e.detail.value);
  },
  chooseImage() {
    this.toggle("show",true);
    let that = this;
    wx.chooseImage({
      sourceType: ['album', 'camera'],
      success(res) {
        for (let url of res.tempFilePaths) {
          app.upload(url, function (r) {
            let picList = that.data.picList;
            picList.push(r.message);
            that.toggle("picList", picList);
          })
        }
      }
    })
  },
  login(f) {
    let that = this;
    app.getUserInfo({
      success(res) {
        f && f(res);
      },
      error() {
        console.log("err")
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.toggle("showUrl", app.globalData.showUrl);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    if(this.data.show){
      this.toggle("show",false);
      return
    }
    that.login(function (res) {
      let data = {
        cusCompany: res.cusCompany,
        cusId: res.id,
        cusName: res.cusName,
        cusTel: res.cusTel,
        cusAddress: res.cusAddress,
        cusKtz: res.cusKtz,
        cusEmail: res.cusEmail,
        sampleNum: res.sampleNum
      }
      that.toggle("params", data);
      that.baseInfo();
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})